package org.hit.burkun.network.tester;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;

import org.hit.burkun.file.FileHelper;
import org.hit.burkun.network.SerializableGraph;
import org.hit.burkun.network.Vertex;
import org.hit.burkun.network.models.BiographMethods;
import org.hit.burkun.network.models.RandomWalkWithRestart;
import org.hit.burkun.network.models.StaticMethods;
import org.hit.burkun.network.models.SimplePageRank.VertexWithPr;

public class RandomwalkWithRestartTester {

	public static void main(String[] args) {
		//wrong!!!!
		SerializableGraph sg = StaticMethods.loadGraphNoDir();
		Vertex s = sg.getVertexByUmls("C0030567");
		RandomWalkWithRestart spr1 = new RandomWalkWithRestart(sg, s);
		spr1.setZuni(0.65);
		LinkedList<VertexWithPr> vp1 = spr1.getPageRank(false, 20, 1e-8);
		for(VertexWithPr vp : vp1){
			if(vp.getVertex().getUmlsId().equals("C1538299")){
				System.out.println(vp.getPr());
			}
		}
		//runBiographMethods();
	}
	public static void runBiographMethods(){
		HashSet<String> vs = ValidData.getValidUmls();
		double eps = 1e-12;
		SerializableGraph sg = StaticMethods.loadGraphNoDir();
		HashMap<Vertex, Double> resG = BiographMethods.getGlobalPr(false, sg, 1, eps);
		HashMap<Vertex, Double> resGw = BiographMethods.getGlobalPr(true, sg, 1, eps);
		
		double zuni = 0.05;
		while(zuni < 1){
			HashMap<String, Double> aucMapWeight = new HashMap<>();
			HashMap<String, Double> aucMapNoWeight = new HashMap<>();
			for(String v : vs){
					LinkedList<String> res1 = BiographMethods.doBioGraphMethod(v, resGw, sg, true, zuni);
					LinkedList<String> res2 = BiographMethods.doBioGraphMethod(v, resG, sg, false, zuni);
					zuni += 0.05;
					double top1 = calcAucByVertex(res1);
					double top2 = calcAucByVertex(res2);
					aucMapWeight.put(v, top1);
					aucMapNoWeight.put(v, top2);
			}
			saveRecallAucAndStd(aucMapNoWeight.values(), zuni, false);
			saveRecallAucAndStd(aucMapWeight.values(), zuni, true);
		}
		
		
	}
	
	private static double calcAucByVertex(LinkedList<String> res){
		double[] tops = new double[res.size()];
		int counter = 0;
		for(String line : res){
			String[] items = line.split("\\$");
			double top = Double.parseDouble(items[2]);
			tops[counter++] = top;
		}
		double sums = 0;
		for(int i=0; i <= 10000; i++){
			counter = 0;
			for(double value : tops){
				if(value <= i/10000.0){
					counter += 1;
				}
			}
			sums += counter;
		}
		double a = sums / 10000 / tops.length;
		return a;
	}
	
	private static void saveRecallAucAndStd(Collection<Double> collection, double zuni, boolean useWeight){
		double sums = 0;
		for(double v : collection){
			sums += v;
		}
		double mean = sums / collection.size();
		double var = 0;
		for(double v : collection){
			var += (v-mean)*(v-mean);
		}
		double std = Math.sqrt(var)/collection.size();
		LinkedList<Double> ls = new LinkedList<>();
		ls.addAll(collection);
		FileHelper.writeFile("data/ranking/all-"+"mean-auc-"+mean+ "-std-" +std+ "-weight-"+useWeight+"-zuni-"+zuni+".txt" , ls);
		
	}
}
